package asynqmon

import (
	"net/http"

	_ "gitee.com/huangxinbo/xcas/internal/app/docs" // Import docs package
	"gitee.com/huangxinbo/xcas/internal/config"
	"github.com/hibiken/asynq"
	"github.com/hibiken/asynqmon"
	"github.com/spf13/cobra"
)

var (
	StartCmd = &cobra.Command{
		Use:     "asynqmon",
		Short:   "Start asynqmon",
		Example: "cas asynqmon",
		Run: func(cmd *cobra.Command, args []string) {
			run()
		},
	}
)

func run() {
	cfg := config.InitConfig()

	h := asynqmon.New(asynqmon.Options{
		RootPath: "/monitoring", // RootPath specifies the root for asynqmon app
		RedisConnOpt: asynq.RedisClientOpt{
			Addr:     cfg.Redis.Addr,
			Username: cfg.Redis.Username,
			Password: cfg.Redis.Password,
			DB:       cfg.Asynq.DB,
		},
	})

	// Note: We need the tailing slash when using net/http.ServeMux.
	http.Handle(h.RootPath()+"/", h)

	// Go to http://localhost:3000/monitoring to see asynqmon homepage.
	http.ListenAndServe(":3000", nil)
}
